package control;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionMapping;

import model.UsuarioCT;

public class FiltroSeguranca implements Filter{
	
	private FilterConfig filterConfig = null; 

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
		
	}
	
	@Override
	public void destroy() {
		this.filterConfig = null;
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

		HttpSession session = ((HttpServletRequest)req).getSession();
		UsuarioCT usuario = (UsuarioCT)session.getAttribute("usuario");

		if (usuario == null){
			HttpServletResponse rp = (HttpServletResponse)res;
			//rp.sendRedirect("./");
			req.getRequestDispatcher("./").forward(req, res);

		}else{
			chain.doFilter(req, res);
		}
	}
}